package com.zf.yichat.controller.trend;

import com.github.pagehelper.PageHelper;
import com.zf.yichat.controller.BaseController;
import com.zf.yichat.mapper.TrendMapper;
import com.zf.yichat.model.Trend;
import com.zf.yichat.utils.common.DtoChangeUtils;
import com.zf.yichat.utils.response.FsResponse;
import com.zf.yichat.utils.response.FsResponseGen;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import java.util.Objects;

/**
 * 一句话描述功能
 *
 * @author fengsong
 * @date create in 10:34 2019/9/6 2019
 */
@Controller
@RequestMapping("trend")
public class TrendController extends BaseController {

    @Autowired
    private TrendMapper trendMapper;

    @RequestMapping("index")
    public String index() {
        return "trend/index";
    }


    @RequestMapping("/list")
    @ResponseBody
    public FsResponse list(Integer page, Integer limit, Long userId, Integer publicStatus) {

        Example example = new Example(Trend.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", 0);

        if (Objects.nonNull(userId)) {
            criteria.andEqualTo("userId", userId);
        }
        if (Objects.nonNull(publicStatus)) {
            criteria.andEqualTo("publicStatus", publicStatus);
        }
        example.setOrderByClause(" order_weight asc, id desc");
//        example.setOrderByClause(" ctime desc");
        PageHelper.startPage(page, limit);
        return DtoChangeUtils.getPageList(trendMapper.selectByExample(example), v -> v);
    }


    @RequestMapping("/public/set")
    @ResponseBody
    public FsResponse set(Long id) {

        Trend t = new Trend();
        t.setId(id);
        t.setPublicStatus(1);
        trendMapper.updateByPrimaryKeySelective(t);
        return FsResponseGen.success();
    }


    @RequestMapping("/public/cancle")
    @ResponseBody
    public FsResponse cancle(Long id) {

        Trend t = new Trend();
        t.setId(id);
        t.setPublicStatus(0);
        trendMapper.updateByPrimaryKeySelective(t);
        return FsResponseGen.success();
    }

    /**
     * 删除动态列表
     * @param id
     * @return
     */
    @RequestMapping("/public/delete")
    @ResponseBody
    public FsResponse delete(Long id) {
        trendMapper.deleteByPrimaryKey(id);
        return FsResponseGen.success();
    }

    /**
     * 设置动态列表的排序权重
     * @param id
     * @return
     */
    @RequestMapping("/public/orderWeight")
    @ResponseBody
    public FsResponse orderWeight(Long id, Integer orderWeight) {

        trendMapper.setTrendOrderWeight(id, orderWeight);
        return FsResponseGen.success();
    }
}
